import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class ControladorCliente {
	
	public void insertarCliente(Connection conexion,Cliente c){
		
		Statement st= null;
		
		try{
			st=conexion.createStatement();
			st.executeUpdate("INSERT INTO cliente VALUES('"+c.getDni()+"','"+c.getNombre()+"','"+c.getApellido()+"','"+c.getTelefono()+"')");
		}catch(SQLException s){
			System.out.println("Error SQL"+s.getMessage());
		}finally{
			try{
			st.close();
			}catch(SQLException q){
				System.out.println("Error de MYSQL: No se ha podido cerrar la conexion");
			}
		}
	}
	
	public void listarClientes(Connection conexion){
		
		Statement st= null;
		ResultSet rs= null;
		
		try{
			st= conexion.createStatement();
			rs= st.executeQuery("SELECT * FROM cliente");
			
			System.out.println("================================");
			System.out.println("Lista de Clientes: ");
			System.out.println("--------------------------------");
			
			while(rs.next()){
				System.out.println("DNI: "+rs.getObject("dni")+", Nombre: "+rs.getObject("nombre")+", Apellido: "
						+rs.getObject("apellido")+", Telefono: "+rs.getObject("telefono"));
			}
		}catch(SQLException e){
			System.out.println("Error SQL"+e.getMessage());
		}finally{
			try{
				st.close();
				rs.close();
			}catch(SQLException ce){
				System.out.println("Error de MYSQL: No se ha podido cerrar la conexion");
			}
		}
	}

	public void mostrarClienteDni(Connection conexion,String dni){
		
		Statement st=null;
		ResultSet rs=null;
		
		try{
			st=conexion.createStatement();
			rs=st.executeQuery("SELECT * from cliente WHERE dni='"+dni+"'");
			
			System.out.println("================================");
			System.out.println("Cliente buscado por DNI: ");
			System.out.println("--------------------------------");
			
			while(rs.next()){
				System.out.println("DNI: "+rs.getObject("dni")+", Nombre: "+rs.getObject("nombre")+", Apellidos: "
						+rs.getObject("apellido")+", Telefono: "+rs.getObject("telefono"));
			}
		}catch(SQLException r){
			System.out.println("Error SQL"+r.getMessage());
		}finally{
			try{
				st.close();
				rs.close();
			}catch(SQLException e){
				System.out.println("Erros de MYSQL: No se ha podido cerrar la conexion");
			}
		}
	}
}
